<template>
  <section class="diseasesHealth">
    <div class="operate">
      <ElButton type="primary" round plain @click="upMassagetPrice"
        >保存修改</ElButton
      >
      <ElButton type="primary" round plain @click="exportExcel"
        >导出表格</ElButton
      >
    </div>
    <MemberSelector
      class="members"
      :no-members.sync="noMembers"
      @selected="filter"
    />
    <ElTable
      :class="{noMembers: noMembers}"
      :data="showCommonDisease"
      :stripe="true"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <ElTableColumn align="center" type="selection" width="60">
      </ElTableColumn>
      <ElTableColumn
        align="center"
        prop="commodity_name"
        width="130"
        label="项目名称"
      >
      </ElTableColumn>
      <ElTableColumn
        align="center"
        prop="price_primary"
        min-width="180"
        label="初级服务"
      >
        <template slot-scope="scope">
          <ElInput
            v-model="scope.row.price.price_primary"
            placeholder="0"
            class="input"
            @change="changeEditStatus"
            @input="ivalidNumber"
            >{{ scope.row.price.price_primary }}</ElInput
          >元/次
        </template>
      </ElTableColumn>
      <ElTableColumn
        align="center"
        prop="price_advanced"
        min-width="180"
        label="高级服务"
      >
        <template slot-scope="scope">
          <ElInput
            v-model="scope.row.price.price_advanced"
            placeholder="0"
            class="input"
            @change="changeEditStatus"
            >{{ scope.row.price.price_advanced }}</ElInput
          >元/次
        </template>
      </ElTableColumn>
      <ElTableColumn
        align="center"
        prop="price_chief"
        min-width="180"
        label="首席服务"
      >
        <template slot-scope="scope">
          <ElInput
            v-model="scope.row.price.price_chief"
            placeholder="0"
            class="input"
            @change="changeEditStatus"
            >{{ scope.row.price.price_chief }}</ElInput
          >元/次
        </template>
      </ElTableColumn>
      <ElTableColumn align="center" prop="status" width="120" label="使用状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '已上架' : '已下架' }}
        </template>
      </ElTableColumn>
      <ElTableColumn align="center" prop="methods" min-width="180" label="操作">
        <template slot-scope="scope">
          <ElButton
            size="mini"
            round
            plain
            :type="scope.row.status === 1 ? 'primary' : 'warning'"
            @click="upShelf(scope.row)"
            >{{ scope.row.status === 1 ? '下架' : '上架' }}</ElButton
          >
        </template>
      </ElTableColumn>
    </ElTable>
    <ElPagination
      v-if="commonDisease.length > 20"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      layout="sizes, prev, pager, next"
      :total="commonDisease.length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </ElPagination>
  </section>
</template>

<script>
import _ from 'lodash';
import {pageSizes} from '@/store';
import {http, catchAppError} from '@/util';
import url from '@/config';
import MemberSelector from './MemberSelector.vue';
import toast from '@/toast';
import changeEditStatus from './ProjectEditStatus';

export default {
  name: 'DiseasesHealth',
  components: {MemberSelector},
  props: {
    type: {type: String, default: 'diseases'}
  },
  data() {
    return {
      pageSize: 20,
      currentPage: 1,
      pageSizes,
      commonDisease: [],
      params: {
        shop_type: 1,
        grade_id: null
      },
      noMembers: true,
      showCommonDisease: [],
      multipleSelection: []
    };
  },
  beforeMount() {
    if (this.type === 'health') {
      this.params.shop_type = 2;
    }
    this.getDiseases(this.params);
  },
  updated() {
    changeEditStatus(false);
  },
  methods: {
    changeEditStatus,
    ivalidNumber(value) {
      if (+value < 0 || !_.isNumber(value)) {
        value = '0.00';
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection[this.currentPage - 1] = val;
    },
    exportExcel() {
      const q = _.reduce(this.multipleSelection, (a, b) => _.concat(a, b), []);
      const idList = q.map(({id}) => id);
      const params = {
        isAll: 2,
        type: this.type === 'diseases' ? 1 : 2,
        idList: []
      };
      if (idList.length === this.commonDisease.length) {
        params.isAll = 1;
      } else {
        params.idList = idList;
      }
      if (!params.idList.length && params.isAll === 2) {
        toast('导出表格勾选内容不能为空！');
        return;
      }
      http
        .post(url.exportExcel, params)
        .then(res => {
          window.location.href = `//${res.url}`;
        })
        .catch(catchAppError);
    },
    handleCurrentChange(val) {
      this.showCommonDisease = _.chunk(this.commonDisease, this.pageSize)[
        val - 1
      ];
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.handleCurrentChange(1);
      this.multipleSelection = [];
    },
    getDiseases() {
      http
        .post(url.diseases, this.params)
        .then(res => {
          this.commonDisease = res;
          this.handleCurrentChange(1);
        })
        .catch(catchAppError);
    },
    filter(id) {
      this.params.grade_id = id;
      this.getDiseases();
    },
    upShelf(product) {
      http
        .post(url.upShelf, {
          commodity_id: product.id,
          status: `${product.status + 1}`
        })
        .then(() => {
          // eslint-disable-next-line no-param-reassign
          product.status = +!product.status;
          // toast('信息修改成功！');
        })
        .catch(catchAppError);
    },
    upMassagetPrice() {
      const params = {
        data: this.showCommonDisease.map(({price, id}) => ({...price, id}))
      };
      http
        .post(url.upMassagetPrice, params)
        .then(() => {
          toast('信息修改成功！');
          changeEditStatus(false);
        })
        .catch(catchAppError);
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
.diseasesHealth {
  position: relative;
  .operate {
    position: absolute;
    right: 0;
    top: -45px;
  }
}
</style>
